package com.wdhcr.controller;

import com.wdhcr.entity.Blog;
import com.wdhcr.entity.Type;
import com.wdhcr.enums.ResultCode;
import com.wdhcr.service.BlogService;
import com.wdhcr.service.TypeService;
import com.wdhcr.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*
 * @description: 展示分类页的控制层
 * @author: wdhcr
 * @dateTime: 2020/3/24 21:27
 */
@RestController

public class ShowTypeController {



    @Autowired
    private BlogService blogService;
    @Autowired
    private TypeService typeService;

    /*
     * @description: 分类页的分类列表的请求接口
     * @params: []
     * @return: com.wdhcr.utils.ResponseResult
     * @author: wdhcr
     * @dateTime: 2020/3/24 21:30
     */
    @CrossOrigin
    @GetMapping("showTypeList")
    public ResponseResult showTypeList(){

        List<Type> types = typeService.listType();
        if (types !=null){
            return new ResponseResult(ResultCode.SUCCESS.getCode(),"获取成功!",types);
        }
        return new ResponseResult(ResultCode.SUCCESS.getCode(),"获取失败!",null);
    }
    
    @CrossOrigin
    @PostMapping("showBlogByType/{page}/{size}")
    public ResponseResult showBlogByType(@PathVariable("page") Integer page,@PathVariable("size") Integer size,@RequestBody Type type){
        //根据更新时间倒叙排列
        Sort update_time = Sort.by(Sort.Direction.DESC,"updateTime");
        PageRequest pageRequest = PageRequest.of(page, size,update_time);
        Page<Blog> blogByType = blogService.findBlogByType(pageRequest, type);
        if (blogByType !=null){
            return new ResponseResult(ResultCode.SUCCESS.getCode(),"获取成功!",blogByType);
        }
        return new ResponseResult(ResultCode.SUCCESS.getCode(),"获取成功!",null);
    }
}
